// packageB/pages/page_coupon/page_coupon.js
import { ml_hideLoading, ml_showLoading, ml_showToast, ml_Toast } from '../../../utils/asyncWX'
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    coupon_list: null
  },

  Collar_roll(e) {
    let Collar_inf = e.currentTarget.dataset.item;
    let login_openid = wx.getStorageSync('openid');
    let seting = { 'usetype': 'collar', 'collar_id': Collar_inf._id, 'collar_inf': Collar_inf, 'login_openid': login_openid, 'receive_time': +new Date() };
    ml_showLoading();
    wx.cloud.callFunction({ name: 'collar_roll', data: seting }).then(res => {
      console.log(res);
      if (res.result.code == 200) {
        let text = res.result.text;
        // this.getCollar_roll();
        wx.navigateBack({
          delta: 1,
        })
        ml_showToast(text);
      } else if (res.result.code == 400) {
        ml_Toast(res.result.text);
      }
    }).catch(res => {
      ml_Toast('请求数据失败');
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let give_String_loop = options.give_String;
    let give_String_app = app.globalData.give_String;
    app.globalData.give_String = null;
    let give_String_cun = wx.getStorageSync('give_String');
    wx.setStorageSync('give_String', '');
    console.log(give_String_loop, give_String_app, give_String_cun);
    if (give_String_loop == give_String_app) {
      if (give_String_loop == give_String_cun) {
        //请求优惠卷
        wx.cloud.callFunction({
          name: 'give_coupon_page', data: {
            type: 'getcoupon_list'
          }
        }).then(res => {
          let data = res.result.data;
          this.return_time(data);
          this.setData({ coupon_list: data[0] });
        })
      }
    }
  },
  //整理时间字符
  return_time(object) {
    for (let i = 0; i < object.length; i++) {
      let time_1 = object[i].over_time;
      let unixTimestamp = new Date(time_1);
      let commonTime = unixTimestamp.toLocaleString();
      object[i]['return_time'] = commonTime;
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})